﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Sprout.Core.Security
{
    public class MD5Helper
    {
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="data">需要加密的数据</param>
        /// <returns>32位加密字符</returns>
        public static string Encrypt(string data)
        {
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
            byte[] hashValue = cryptoServiceProvider.ComputeHash(buffer);

            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte byteData in hashValue)
            {
                if (byteData < 16)
                {
                    stringBuilder.Append("0");
                }
                stringBuilder.Append(byteData.ToString("X"));
            }
            return stringBuilder.ToString().ToLower();
        }
        ///// <summary>
        ///// MD5 加密
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="length">加密长度两个值 16或32</param>
        ///// <returns>加密字符</returns>
        //public static string Encrypt(string data, int length = 32)
        //{
        //    StringBuilder stringBuilder = new StringBuilder();
        //    byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes(data);
        //    byte[] dataOutput = MD5.Create().ComputeHash(dataToHash);
        //    switch (length)
        //    {
        //        case 16:
        //            for (int i = 4; i < 12; i++)
        //                stringBuilder.Append(dataOutput[i].ToString("X2"));
        //            break;
        //        case 32:
        //            for (int i = 0; i < 16; i++)
        //                stringBuilder.Append(dataOutput[i].ToString("X2"));
        //            break;
        //        default:
        //            throw new Exception("length只能传入16或32");
        //    }
        //    return stringBuilder.ToString().ToLower();
        //}

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="data">需要加密的数据</param>
        /// <param name="encryptTimes">加密次数</param>
        /// <returns>32位加密字符</returns>
        public static string Encrypt(string data, int encryptTimes)
        {
            string encryptData = data;
            do
            {
                encryptTimes--;
                encryptData = Encrypt(encryptData);
            } while (encryptTimes > 0);

            return encryptData;
        }

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="data">需要加密的数据</param>
        /// <param name="confuseData">混淆字符</param>
        /// <param name="encryptTimes">加密次数</param>
        /// <returns>64位加密字符</returns>
        public static string Encrypt(string data, string confuseData, int encryptTimes = 2)
        {
            string encryptData = data;
            string encryptConfuseData = confuseData;
            do
            {
                encryptTimes--;
                encryptData = Encrypt(encryptData);
                encryptConfuseData = Encrypt(encryptConfuseData);
            } while (encryptTimes > 0);

            encryptData = encryptData.Insert(0, encryptConfuseData.Substring(24));
            encryptData = encryptData.Insert(32, encryptConfuseData.Substring(0, 8));
            encryptData = encryptData.Insert(16, encryptConfuseData.Substring(8, 8));
            encryptData = encryptData.Insert(32, encryptConfuseData.Substring(16, 8));
            return encryptData;
        }
    }
}
